<?php namespace Router;
//    $router = [
//        'user' => [ - модуль контроллера
//            'index', - контроллер
//            'faq'
//        ],
//        'api' => [
//            'index'
//        ],
//        'admin' => [
//
//        ]
//    ];

    class Routers {
        protected $URL;
        protected $ROUTE;
        protected $CONTROLLER;
        protected $VIEW;

        public function __construct() {
            $this->URL = $_SERVER['REQUEST_URI'];
            $this->CONTROLLER = __DIR__.'/controller';
            $this->VIEW = __DIR__.'/view';
        }

        public function createRouter() {
            $moduleName = '';
            $defaultController = 'indexController.php';
            $defaultView = 'index.php';

            $clearUrl = explode('/', $this->URL);
            unset($clearUrl[0]);
            $moduleName = ($clearUrl[1] != 'api' && $clearUrl[1] != 'admin') ? $moduleName = 'user' : $moduleName = $clearUrl[1];
            $this->route($moduleName, $clearUrl);
        }

        protected function route($moduleName, $clearUrl = []) {
            if ($moduleName == 'user'){
                $controller = ($clearUrl[1] != '') ? $controller = $clearUrl[1] : $controller = 'index';
                $action = (isset($clearUrl[2])) ? $action = $clearUrl[2] : false;
                $params = (isset($clearUrl[3])) ? $params = $clearUrl[3] : false;

                $tempControl = $this->CONTROLLER.'/'.$moduleName.'/'.$controller.'Controller.php';
                $tempView = $this->VIEW.'/'.$moduleName.'/'.$controller.'.php';

                if (file_exists($tempControl) && file_exists($tempView)){
                    include $tempView;
                    include $tempControl;
                } else {
                    include $this->CONTROLLER.'/'.$moduleName.'/'.'index'.'Controller.php';
                    include $this->VIEW.'/'.$moduleName.'/'.'index'.'.php';
                    $controller = 'index';
                }

                $controller = $controller.'Controller';
                $controller = new $controller();

                if ($action != false){
                    $action = $action.'Action';
                    $controller->$action();

                    if ($params != false) {

                    }
                }
            } else if ($moduleName == 'api'){
                $controller = (isset($clearUrl[2])) ? $controller = $clearUrl[2] : $controller = 'index';
                $action = (isset($clearUrl[3])) ? $action = $clearUrl[3] : "index";
                $params = (isset($clearUrl[4])) ? $params = $clearUrl[4] : false;

                include $this->CONTROLLER.'/'.$moduleName.'/'.$controller.'Controller.php';
                $controller = $controller.'Controller';
                $controller = new $controller();

                if ($params != false){
                    $action = $action.'Action';
                    $controller->$action($params);
                } else {
                    $action = $action.'Action';
                    $controller->$action();
                }

            } else if ($moduleName == 'admin') {
                $controller = (isset($clearUrl[2]) && $clearUrl[2] != "") ? $controller = $clearUrl[2] : $controller = 'index';
                $action = (isset($clearUrl[3])) ? $action = $clearUrl[3] : "index";
                $params = (isset($clearUrl[4])) ? $params = $clearUrl[4] : false;
                $view = $controller;

                include $this->CONTROLLER.'/'.$moduleName.'/'.$controller.'Controller.php';


                $controller = $controller.'Controller';
                $controller = new $controller();

                if ($params != false){
                    $action = $action.'Action';
                    $controller->$action($params);
                } else {
                    $action = $action.'Action';
                    $controller->$action();
                }

                include $this->VIEW.'/'.$moduleName.'/'.$view.'.php';
            }
        }

        protected function moduleControll() {
            $module = scandir($this->CONTROLLER.'/');
            foreach ($module as $key => $item) {
                if ($item == '.' || $item == '..'){
                    continue;
                }
                $controller = scandir($this->CONTROLLER.'/'.$item);
                $view = [];
                if ($item == 'api'){
                    $this->addRoute($item, $controller);
                    continue;
                }
                $view = scandir($this->VIEW.'/'.$item);
                $this->addRoute($item, $controller, $view);
            }
        }

        protected function addRoute($url, $controller, $view = []) {
            $this->ROUTE[$url] = [
                "controller" =>  $controller,
                "view" => $view
            ];
        }

        protected function checkControl($module, $controller, $view) {
            if ($module != 'api'){
                try {
                    include $this->CONTROLLER.'/'.$module.'/'.$controller;
                    include $this->VIEW.'/'.$module.'/'.$view;
                } catch (Exception $e) {

                }
            } else {
                try {
                    include $this->CONTROLLER.'/'.$module.'/'.$controller;
                } catch (Exception $e) {

                }
            }
        }
    }